import { store } from '@/store'
import { defineStore } from 'pinia'
import { useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()

export const useDebugStore = defineStore('debug', {
  state: () => ({
    queryObj: {
      query: {},
      requestContentType: "",
      requestParamsTable: [],
      requestBodyTable: [],
      requestJSON: "",
      requestHeader: [],
      requestAuth: "",
      cookie: [],
    },
    response: {
      responseRequestHeader: [],
      responseRequestBody: "",
      responseRequestParams: [],
      responseHeader: [],
      responseJSON: "",
      responseResult: {},
    },
    environmentId: "",
    selectTreeId: "",
    treeData: JSON.parse(wsCache.get('treeData')) || [],
    environmentVars: [],
    environments: [],
    cardList: JSON.parse(wsCache.get('cardList')) || [],
    list: JSON.parse(wsCache.get('list')) || [{
      id: 0,
      title: '项目概览',
      icon: '',
      desc: '统计所在项目的资源及分布的数据统计',
      size: 24
    },{
      id: 1,
      title: '接口数',
      icon: '',
      desc: '统计接口总数及接口覆盖率和完成率数据统计',
      size: 24
    },
    {
      id: 2,
      title: '接口用例数',
      icon: '',
      desc: '接口用例总数及接口完成情况的数据统计',
      size: 12
    },
    {
      id: 3,
      title: '场景数',
      icon: '',
      desc: '场景总数及执行和完成的数据统计',
      size: 12
    }]
  }),
  getters: {
    getQueryObj(): object {
      return this.queryObj
    },
    getResponse(): object {
      return this.response
    },
    getQuery(): object {
      return this.queryObj.query
    },
    getCookie(): any {
      return this.queryObj.cookie
    },
    getRequestContentType(): string {
      return this.queryObj.requestContentType
    },
    getRequestParamsTable(): any {
      return this.queryObj.requestParamsTable
    },
    getRequestBodyTable(): any {
      return this.queryObj.requestBodyTable
    },
    getRequestJSON(): any {
      return this.queryObj.requestJSON
    },
    getRequestHeader(): any {
      return this.queryObj.requestHeader
    },
    getRequestAuth(): any {
      return this.queryObj.requestAuth
    },
    getResponseRequestHeader(): any {
      return this.response.responseRequestHeader
    },
    getResponseRequestBody(): any {
      return this.response.responseRequestBody
    },
    getResponseRequestParams(): any {
      return this.response.responseRequestParams
    },
    getResponseHeader(): any {
      return this.response.responseHeader
    },
    getResponseJSON(): any {
      return this.response.responseJSON
    },
    getResponseResult(): any {
      return this.response.responseResult
    },
    getEnvironmentId(): any {
      return this.environmentId
    },
    getSelectTreeId(): string {
      return this.selectTreeId
    },
    getTreeData(): any {
      return this.treeData
    },
    getEnvironmentVars(): any {
      return this.environmentVars
    },
    getEnvironments(): any {
      return this.environments
    },
    getCardList(): any {
      return this.cardList
    },
    getList(): any {
      return this.list
    }
  },
  actions: {
    setQuery(data) {
      this.queryObj.query = data
    },
    setCookie(data) {
      this.queryObj.cookie = data
    },
    setRequestContentType(data) {
      this.queryObj.requestContentType = data
    },
    setRequestParamsTable(data) {
      this.queryObj.requestParamsTable = data
    },
    setRequestBodyTable(data) {
      this.queryObj.requestBodyTable = data
    },
    setRequestJSON(data) {
      this.queryObj.requestJSON = data
    },
    setRequestHeader(data) {
      this.queryObj.requestHeader = data
    },
    setRequestAuth(data) {
      this.queryObj.requestAuth = data
    },
    setResponseRequestHeader(data) {
      this.response.responseRequestHeader = data
    },
    setResponseRequestBody(data) {
      this.response.responseRequestBody = data
    },
    setResponseRequestParams(data) {
      this.response.responseRequestParams = data
    },
    setResponseJSON(data) {
      this.response.responseJSON = data
    },
    setResponseHeader(data) {
      this.response.responseHeader = data
    },
    setResponseResult(data) {
      this.response.responseResult = data
    },
    setEnvironmentId(data) {
      this.environmentId = data
    },
    setSelectTreeId(data) {
      this.selectTreeId = data
    },
    setTreeData(data) {
      wsCache.set('treeData', JSON.stringify(data))
      this.treeData = data
    },
    setEnvironmentVars(data) {
      this.environmentVars = data
    },
    setEnvironments(data) {
      this.environments = data
    },
    setCardList(data) {
      wsCache.set('cardList', JSON.stringify(data))
      this.cardList = data
    },
    setList(data) {
      wsCache.set('list', JSON.stringify(data))
      this.list = data
    }
  }
})

export const useDebugStoreWithOut = () => {
  return useDebugStore(store)
}
